package com.jy.adscm.stock.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.dealer.bean.Dealer;
import com.jy.adscm.dealer.mapper.DealerMapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.hospital.bean.JyHospitalBase;
import com.jy.adscm.hospital.mapper.JyHospitalBaseMapper;
import com.jy.adscm.order.bean.JyProduct;
import com.jy.adscm.order.mapper.JyProductMapper;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.stock.bean.JyInStorage;
import com.jy.adscm.stock.bean.JyOutStorage;
import com.jy.adscm.stock.mapper.JyInStorageMapper;
import com.jy.adscm.stock.mapper.JyOutStorageMapper;
import com.jy.adscm.stock.service.JyOutStorageService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-01-23
 */
@Service
public class JyOutStorageServiceImpl extends ServiceImpl<JyOutStorageMapper, JyOutStorage> implements JyOutStorageService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JyProductMapper jyProductMapper;
    @Autowired
    JyHospitalBaseMapper jyHospitalBaseMapper;
    @Autowired
    JyCodeMapper jyCodeMapper;
    @Autowired
    DealerMapper dealerMapper;
    @Autowired
    JyInStorageMapper jyInStorageMapper;
    @Override
    public boolean insertJyOutStorage(JyOutStorage jyOutStorage, Token token)throws AdscmSystemException {
        jyOutStorage.setOutStoragePerson(token.getUser().getId().toString());
        jyOutStorage.setOutStorageTime(new Date());
        jyOutStorage.setDealer(token.getUser().getUnitId().toString());
        boolean insert = super.insert(jyOutStorage);
        return insert;
    }

    @Override
    public boolean deleteJyOutStorage(Integer id, Token token) throws AdscmSystemException{
        if (token.getUser().getUnitId()==0){

            throw  new AdscmSystemException("您不能删除此信息");
        }
        super.deleteById(id);
        return false;
    }

    @Override
    public Boolean updateJyOutStorage(JyOutStorage jyOutStorage, Token token)throws AdscmSystemException {
        boolean update = super.updateById(jyOutStorage);
        return update;
    }

    @Override
    public List<JyOutStorage> selectAll(Token token)throws AdscmSystemException {
        List<JyOutStorage>  jyOutStorages= new ArrayList<>();
           if (token.getUser().getUnitId()==0){
               jyOutStorages = super.selectList(null);
           } else {
              Wrapper<JyOutStorage> wrapper =new EntityWrapper<>();
              wrapper.eq("dealer",token.getUser().getUnitId());
              jyOutStorages = super.selectList(wrapper);
           }
        for (JyOutStorage jyOutStorage : jyOutStorages) {
             if (AdscmBaseUtils.getInstance().isNotEmpty(jyOutStorage.getHospital(),null)){
                 JyHospitalBase jyHospitalBase = jyHospitalBaseMapper.selectById(jyOutStorage.getHospital());
                 jyOutStorage.setHospitalName(jyHospitalBase.getHospitalName());
             }
             if (AdscmBaseUtils.getInstance().isNotEmpty(jyOutStorage.getDepartment(),null)){
                      JyCode jyCode =new JyCode();
                      jyCode.setCode(jyOutStorage.getDepartment());
                      jyCode.setType(4);
                      jyCode = jyCodeMapper.selectOne(jyCode);
                 jyOutStorage.setDepartmentName(jyCode.getName());
             }
             if (AdscmBaseUtils.getInstance().isNotEmpty(jyOutStorage.getOutStoragePerson(),null)){
                 JyUser jyUser = jyUserMapper.selectById(jyOutStorage.getOutStoragePerson());
                 jyOutStorage.setOutStoragePersonName(jyUser.getName());
             }
             if (AdscmBaseUtils.getInstance().isNotEmpty(jyOutStorage.getProduct(),null)){
                 JyProduct jyProduct = jyProductMapper.selectById(jyOutStorage.getProduct());
                 jyOutStorage.setProductName(jyProduct.getName());
             }
             if (AdscmBaseUtils.getInstance().isNotEmpty(jyOutStorage.getDealer(),null)){
                 Dealer dealer = dealerMapper.selectById(jyOutStorage.getDealer());
                 jyOutStorage.setDealerName(dealer.getName());
             }
          }
        return  jyOutStorages ;
    }

    @Override
    public List<JyProduct> selectProduct( Token token) {
        List<JyProduct> jyProducts =new ArrayList<>();
        List<JyInStorage> jyInStorages = jyInStorageMapper.queryDealerHaveProduct(token.getUser().getUnitId().toString());
           if (jyInStorages.size()>0){
               for (JyInStorage jyInStorage : jyInStorages) {
                   if (AdscmBaseUtils.getInstance().isNotEmpty(jyInStorage.getProduct(),null)){
                       JyProduct jyProduct = jyProductMapper.selectById(jyInStorage.getProduct());
                       jyProducts.add(jyProduct);
                   }

               }

           }
        return jyProducts;
    }
}
